//
//  THJPurchaseVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/10.
//  Copyright © 2017年 河南德艺.   All rights reserved.
//

#import "THJPurchaseVC.h"
#import "THJKindVC.h"
#import "SDAutoLayout.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "ImagePickerViewController.h"
#import "JPNavigationController.h"
#import "THJAllClassVC.h"
#import "THJAddressSelectVC.h"
#import "THJPickerView.h"
#import "NSString+String.h"
@interface THJPurchaseVC ()<UITextViewDelegate,THJAllClassVCDelegate,THJAddressSelectVCDelegate,THJPickerViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{

    
    NSString * addressVal;
    NSString * guigeVal;
    NSString * fenLeiVal;
    NSInteger imgVTapIndex;
}
@property(nonatomic,strong)UIButton * addImgBtn;
@end
@implementation THJPurchaseVC
-(UIButton*)addImgBtn{
    if (!_addImgBtn) {
        _addImgBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_addImgBtn setBackgroundImage:kGetImage(@"添加图片") forState:UIControlStateNormal];
    }
    return _addImgBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:self.PTitle];
    }else{
        [self setNavBarWithTitle:self.PTitle];
    }
    self.fenLeiLabel.userInteractionEnabled = YES;
    [self labelAddGesture];
//    [self setNavRightFabuBtn];
    self.tunValTF.keyboardType = UIKeyboardTypeNumberPad;
    self.priceTF.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
//给分类种类label添加单击手势
-(void)labelAddGesture{
    NSArray * labelArr = @[self.fenLeiLabel,self.addressLab,self.ggLab];
    for (NSInteger i = 0; i<labelArr.count; i++) {
        UILabel * label = labelArr[i];
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)];
        label.tag = i+100;
        [label addGestureRecognizer:tap];
    }
    NSArray * imgArr = @[self.imgV1,self.imgV2,self.imgV3];
    for (NSInteger i = 0; i<imgArr.count; i++) {
        UIImageView * imgV = imgArr[i];
        imgV.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgVTapAction:)];
        imgV.tag = i+1000;
        [imgV addGestureRecognizer:tap];
    }
}
-(void)labelTapAction:(UITapGestureRecognizer*)tap{
    NSInteger index = tap.view.tag;
    //如果等于0 点击的是分类 以此类推
    if (index == 100) {
        THJAllClassVC * vc = [THJAllClassVC new];
        vc.delegate = self;
        vc.midType = @"5";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (index == 101){
        THJAddressSelectVC * vc = [[THJAddressSelectVC alloc] init];
        vc.delegate = self;
        vc.urlStr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=Area&r=Area"];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index == 102){
        THJPickerView * picker = [[THJPickerView alloc] initWithFrame:self.view.frame];
        picker.dataArr = @[@"标准块10-70mm",@"标准块10-50mm",@"自然块",@"粒(0.5-1mm)",@"粒(1-3mm)",@"粒(3-8mm)",@"粉",@"面"];
        picker.delegate = self;
        [self.view addSubview:picker];
    }
}
#pragma mark ------ 分类 delegate ----
-(void)itemSelectName:(NSString *)name CatId:(NSString *)catId{
    NSLog(@"%@***%@",name,catId);
    self.fenLeiLabel.text  = name;
    fenLeiVal = catId;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark ------ 地区 delegate ----
-(void)selectedAddress:(NSMutableString *)address AreaId:(NSString *)areaId{
    addressVal = areaId;
    self.addressLab.text = address;
    NSLog(@"address-%@",address);
}
#pragma mark ------ 规格 delegate ----
-(void)selectedItem:(NSString*)item Index:(NSInteger)index{
    self.ggLab.text = item;
    guigeVal = [THJTools guigeWithString:item];
}
-(void)imgVTapAction:(UITapGestureRecognizer*)tap{
    imgVTapIndex = tap.view.tag - 1000;
    [self alertWithTitle1:@"取消" title2:@"拍照" title3:@"相册"];
}
#pragma mark ----更多操作---
-(void)actionSheetIndex1Function{
    [self getPhotoWithType:0];
}
-(void)actionSheetIndex2Function{
    [self getPhotoWithType:1];
}

/**
 调用相册或相机
 */
-(void)getPhotoWithType:(NSInteger)index{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (index) {
            case 0:
                //来源:相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                //来源:相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.navigationBar.translucent = NO;

    imagePickerController.sourceType = sourceType;
 
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if(imgVTapIndex == 0){
        self.imgV1.image = image;
    }else if (imgVTapIndex == 1){
        self.imgV2.image = image;
    }else{
        self.imgV3.image = image;
    }
   
}
- (IBAction)sureBtnAction:(id)sender {
    if ([self.titleTF.text isEqual:@""]) {
        [self HUDShowMsg:@"请填写标题" AfterDelayDimiss:1];
        return;
    }else if([self.fenLeiLabel.text isEqual:@"请选择分类"]){
        [self HUDShowMsg:@"请填写分类" AfterDelayDimiss:1];
        return;
    }else if([self.addressLab.text isEqual:@"请选择地址"]){
        [self HUDShowMsg:@"请填写地址" AfterDelayDimiss:1];
        return;
    }else if(!self.tunValTF.text.length){
        [self HUDShowMsg:@"请填写数量" AfterDelayDimiss:1];
        return;
    }else if(!self.contactLab.text.length){
        [self HUDShowMsg:@"请填写联系人" AfterDelayDimiss:1];
        return;
    }else if(!self.phoneTF.text.length){
        [self HUDShowMsg:@"请填写联系电话" AfterDelayDimiss:1];
        return;
    }else if(!self.lastTF.text.length){
        [self HUDShowMsg:@"请输入描述" AfterDelayDimiss:1];
        return;
    }else if(!self.priceTF.text.length){
        [self HUDShowMsg:@"请输入价格" AfterDelayDimiss:1];
        return;
    }else if(![NSString isMobileNumber:self.phoneTF.text]){
        [self HUDShowMsg:@"请输入正确手机号" AfterDelayDimiss:1];
        return;
    }
    NSString *img1 = [self image2DataURL:self.imgV1.image];
    NSString *img2 = [self image2DataURL:self.imgV2.image];
    NSString *img3 = [self image2DataURL:self.imgV3.image];
    NSDictionary * paraDic=@{ @"thumb":img1,
                              @"thumb1":img2,
                              @"thumb2":img3,
                              @"content":self.lastTF.text,
                              @"truename":self.contactLab.text,
                              @"mobile":self.phoneTF.text,
                              @"minamount":self.tunValTF.text,
                              @"gg": [THJTools guigeWithString:self.ggLab.text],
                              @"address":self.addressLab.text,
                              @"catid":fenLeiVal,
                              @"title":self.titleTF.text,
                              @"areaid":addressVal
                              };
    NSString * urlstr = [NSString stringWithFormat:@"%@%@%@",THJUrlDomain,THJindexPHP,@"f=My_sell_5&r=My_sell_5&m=add"];
    NSLog(@"fabu-%@",paraDic);
    __weak typeof(self) weakself = self;
    NSString * token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    [THJNetWork postDataFromUrl:urlstr parameters:paraDic Token:token callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            [weakself performSelector:@selector(pop) withObject:nil afterDelay:1];
        }else{
            [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        }
    }];
    
}
@end
